/*
 *    Copyright 2022 jacky4631 shen ji email:121659050@qq.com
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.dataoke.shoppingguide.app773026.activity.setting;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.dataoke.shoppingguide.app773026.R;
import com.dataoke.shoppingguide.app773026.base.activities.BackActivity;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;

/**
 *
 * @author: shen ji
 * @date: 2022/3/15
 * @email: 121659050@qq.com
 * @wechat: jacky4631
 */
public class FeedBackActivity extends BackActivity implements View.OnClickListener {

    @BindView(R.id.et_feed_back)
    EditText et_feed_back;

    @BindView(R.id.et_mobile)
    EditText mMobile;


    private ProgressDialog mDialog;

    public static void show(Context context) {
        context.startActivity(new Intent(context, FeedBackActivity.class));
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_feed_back;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        setStatusBarDarkMode();
        setDarkToolBar();

    }

    @Override
    protected void initData() {
        super.initData();
    }

    @OnClick({R.id.btn_commit})
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_commit:
                if(!netWorkCheck(FeedBackActivity.this)) {
                    Toast.makeText(FeedBackActivity.this, "网络错误，请重试", Toast.LENGTH_SHORT).show();
                    return;
                }
                String mobile = mMobile.getText().toString().trim();
                String num = "[1][3456789]\\d{9}";
                if(!TextUtils.isEmpty(mobile) && !mobile.matches(num)) {
                    Toast.makeText(FeedBackActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                    return;
                }
                String content = getFeedBackContent();
                if (TextUtils.isEmpty(content)) {
                    Toast.makeText(FeedBackActivity.this, "内容不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                content = "[" + getString(R.string.app_name) + "-功能建议]" + mobile + content;
                addFeedBack(content, null);
                break;
        }
    }

    /**
     * 添加反馈，走系统私信接口
     *
     * @param content content
     * @param file    file
     */
    private void addFeedBack(String content, File file) {
        getDialog().show();
        mMobile.postDelayed(() -> {
            FeedBackActivity.this.finish();
            Toast.makeText(FeedBackActivity.this, "谢谢您的反馈", Toast.LENGTH_SHORT).show();
            mDialog.dismiss();
        }, 500);
    }

    public String getFeedBackContent() {
        return et_feed_back.getText().toString().trim();
    }

    public ProgressDialog getDialog() {
        if (mDialog == null) {
            mDialog = new ProgressDialog(this);
            mDialog.setCancelable(false);
            mDialog.setCanceledOnTouchOutside(false);
        }
        mDialog.setMessage("反馈中...");
        return mDialog;
    }

    // 网络连接判断
    public static boolean netWorkCheck(Context context){
        ConnectivityManager cm =  (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = cm.getActiveNetworkInfo();
        if( info != null ){
            return info.isConnected();
        } else {
            return false;
        }
    }
}
